﻿/// <reference name="MicrosoftAjax.debug.js" />
/// <reference path="../../Navigation/ItemsNavigator.debug.js />

Type.registerNamespace("BM");

// CompareConstraint object

BM.CompareConstraint = function(leafItem, data) {
    BM.CompareConstraint.initializeBase(this, [leafItem, data]);
}

BM.CompareConstraint.prototype = {

    load: function () {
        BM.CompareConstraint.callBaseMethod(this, 'load');
        var itemToCompare = this.getItemToCompare();
        if (itemToCompare != null) {
            itemToCompare.addChanged(Function.createDelegate(this, this.refresh));
        }
    },

    getType: function () {
        ///	<summary>
        ///	returns the data type of the comparison
        ///	</summary>
        ///	<returns type="String" />
        return this.getProperty('type'); ;
    },

    getOperator: function () {
        ///	<summary>
        ///	returns the operator of the comparison
        ///	</summary>
        ///	<returns type="String" />
        return this.getProperty('operator'); ;
    },

    getValueToCompare: function () {
        ///	<summary>
        ///	returns the fixed value to compare
        ///	</summary>
        ///	<returns type="String" />
        return this.getProperty('valueToCompare'); ;
    },

    getItemToComparePath: function () {
        ///	<summary>
        ///	returns the item control path to be compared to
        ///	</summary>
        ///	<returns type="String" />
        return this.getProperty('itemToComparePath');
    },

    getItemToCompare: function () {
        ///	<summary>
        ///	returns the item control to be compared to
        ///	</summary>
        ///	<returns type="MItem" />
        var itemToComparePath = this.getItemToComparePath();
        if (itemToComparePath == null || itemToComparePath == '') {
            return null;
        }
        return BM.ItemsNavigator.selectSingle(this.getItem(), itemToComparePath);
    },

    getCompareTo: function () {
        ///	<summary>
        ///	returns the value to compare, either a fixed value or a value from a comparison item.
        ///	</summary>
        ///	<returns type="String" />
        var itemToCompare = this.getItemToCompare();
        if (itemToCompare == null) {
            return this.getValueToCompare();
        }
        return this._getStringValue(itemToCompare);
    },

    getCompareParameters: function () {
        ///	<summary>
        ///	returns the compare parameters for the generic ASP.NET ValidatorCompare javascript function.
        ///	</summary>
        ///	<returns type="Object" />
        var compareParameters = this.getProperty('compareParameters');
        compareParameters.type = this.getType();
        return compareParameters;
    },

    isMet: function () {
        return ValidatorCompare(this._getStringValue(this.getItem()), this.getCompareTo(), this.getOperator(), this.getCompareParameters());
    },

    getDefaultErrorText: function () {
        return BM.CompareConstraintResource['CompareConstraintError'] + " " + BM.CompareConstraintResource['Not_' + this.getOperator()] + " '" + this.getCompareTo() + "'.";
    },

    _getStringValue: function (item) {
        if (this.getType() == 'String') {
            return item.getValue();
        }

        if (BM.DataType.isDateDataType(item.getDataType())) {
            return item.getValue();
        }

        return item.getDataTypeValue().toString();
    }
}

BM.CompareConstraint.registerClass("BM.CompareConstraint", BM.Constraint);